﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using MongoDB.Driver;
using MongoDB.Bson;

namespace SpiderDataShow.Controllers
{
    public class HomeController : Controller
    {
        /// <summary>
        /// 连接mongodb并检索数据发送给view
        /// </summary>
        /// <returns>ViewResult对象</returns>
        public IActionResult Mgdbtest() {
            //创建数据库客户端连接对象
            var client = new MongoClient("mongodb://localhost:27017");
            //创建数据库对象
            var db = client.GetDatabase("test_db");
            //创建collection对象
            var collection = db.GetCollection<BsonDocument>("test_c");
            //将collection中的文档数据全读出来并转换成json字符串
            //并存入viewdata中，等着在view中可以使用
            ViewData["test_c_docs"] = collection.Find(new BsonDocument()).ToList().ToJson();
            //返回视图
            return View();
        }
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult About()
        {
            ViewData["Message"] = "Your application description page.";

            return View();
        }

        public IActionResult Contact()
        {
            ViewData["Message"] = "Your contact page.";

            return View();
        }

        public IActionResult Error()
        {
            return View();
        }
    }
}
